<!DOCTYPE>
<html>
    <head>
        <meta charset='utf-8'>
        <script src="esl.js"></script>
        <script src="config.js"></script>
        <script src="lib/jquery.min.js"></script>
    </head>
    <body>
        <style>
            html, body, #main {
                width: 100%;
                height: 100%;
            }
        </style>
        <div id="main"></div>
        <script>

            require([
                'echarts',
                'ecStat'
            ], function (echarts, ecStat) {

                var chart = echarts.init(document.getElementById('main'));

                window.onresize = function () {
                        chart.resize();
                };

                $.getJSON('./data/clustering_dataset2.json')
                .done(function (data) {

                    var clusterNumber = 2;
                    
                    // var result = ecStat.clustering.hierarchicalKMeans(data, clusterNumber, false);
                    var result = ecStat.clustering.kMeans(data, clusterNumber);

                    var centroids = result.centroids;

                    var ptsInCluster = result.pointsInCluster;
                    var series = [];

                    for (var i = 0; i < centroids.length; i++) {
                        series.push({
                            name: 'cluster' + i,
                            type: 'scatter',
                            data: ptsInCluster[i],
                            markPoint: {
                                symbolSize: 30,
                                label: {
                                    normal: {
                                        show: false
                                    },
                                    emphasis: {
                                        show: true,
                                        position: 'top',
                                        formatter: function (params) {

                                            return Math.round(params.data.coord[0] * 100) / 100 + '  '
                                                + Math.round(params.data.coord[1] * 100) / 100 + ' ';
                                        },
                                        textStyle: {
                                            color: '#000'
                                        }
                                    }
                                },

                                itemStyle: {
                                    normal: {
                                        opacity: 0.8
                                    }
                                },
                                data: [{
                                    coord: centroids[i]
                                }]
                            }
                        });
                    }

                    chart.setOption({
                        legend: {
                            data: []
                        },
                        tooltip: {
                            trigger: 'axis',
                            axisPointer: {
                                type: 'cross'
                            }
                        },
                        xAxis: {
                            type: 'value'
                        },
                        yAxis: {
                            type: 'value'
                        },
                        series: series
                    });

                });
            });
        </script>
    </body>
</html>